package com.ecnu.sei.app.util;

import java.util.Calendar;

import com.ecnu.sei.app.resources.Time;

public class Utils {

  public enum CollegeYear {
    
    FRESHMAN, SEPHOMORE, JUNIOR, SENIOR;
    
    public static CollegeYear[] getList() {
      return new CollegeYear[]{FRESHMAN, SEPHOMORE, JUNIOR, SENIOR};
    }
  };

  public enum DayHour {
    
    MORN_ONE, MORN_TWO, AFTN_ONE, AFTN_TWO,
    NIGHT, 
    NONE;
  
    public static DayHour[] getList() {
      return new DayHour[]{MORN_ONE, MORN_TWO, AFTN_ONE, AFTN_TWO};
    }
  };

  
  
  // get DayHour from an exam's start time
  public static DayHour getDayHourFromStartTime(long timeInMillis) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timeInMillis);
    return getDayHourFromStartTime(calendar.get(Calendar.HOUR_OF_DAY));
    
  }

  public static DayHour getDayHourFromStartTime(int hourOfDay) {
    switch(hourOfDay) {
    case 8:
      return DayHour.MORN_ONE;
    case 10: 
      return DayHour.MORN_TWO;
    case 13:
      return DayHour.AFTN_ONE;
    case 15:
      return DayHour.AFTN_TWO;
    case 18:
      return DayHour.NIGHT;
    default:
      return DayHour.NONE;
    }
  }

/*  public static Calendar getCalendar(long timeInMillis) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timeInMillis);
    return calendar;
  }
*/
 /* public static String getDateByOffset(long timeInMillis, int weekOfDay) {
    Calendar calendar = getCalendar(timeInMillis);
    calendar.add(Calendar.DAY_OF_WEEK, weekOfDay - 1); 
    return Time.DATE_FORMAT.format(calendar.getTime());
  }*/
  
  public static Time getDateByOffset(long timeInMillis, int offset) {
    Time time = new Time(timeInMillis);
    return new Time(time.getDateTime().plusDays(offset), time.getDayHour());
  }
 
  public static boolean isSameHalfDay(Time t1, Time t2) {
    if (!t1.getDate().equals(t2.getDate())) {
      return false;
    }
    return isSameHalfDay(t1.getDayHour(), t2.getDayHour());
  }
  
  public static boolean isSameHalfDay(DayHour dh1, DayHour dh2) {
    if (dh1 == dh2) {
      return true;
    } else if (dh1 == DayHour.MORN_ONE && dh2 == DayHour.MORN_TWO) {
      return true;
    } else if (dh1 == DayHour.MORN_TWO && dh2 == DayHour.MORN_ONE) {
      return true;
    } else if (dh1 == DayHour.AFTN_ONE && dh2 == DayHour.AFTN_TWO) {
      return true;
    } else if (dh1 == DayHour.AFTN_TWO && dh2 == DayHour.AFTN_ONE) {
      return true;
    }
    
    return false;
  } 
  
  public static boolean isContHalfDay(Time t1, Time t2) {
    if (!t1.getDate().equals(t2.getDate())) {
      return false;
    }
    return isSameHalfDay(t1.getDayHour(), t2.getDayHour());
  }

  public static boolean isContHalfDay(DayHour dh1, DayHour dh2) {
    if (dh1 == DayHour.MORN_TWO && dh2 == DayHour.AFTN_ONE ) {
      return true;
    }
    if (dh1 == DayHour.AFTN_ONE && dh2 == DayHour.MORN_TWO) {
      return true;
    }
    
    return false;
  }
}
